/*******************************************************************/
/*    Copyright (c) 1999-2013 by Spatial Corp.                     */
/*    All rights reserved.                                         */
/*******************************************************************/

//---------------------------------------------------------------------------------------

// SPAXInteroperability
#include "SPAIDocument.h"
#include "SPAIConverter.h"
#include "SPAIFile.h"
#include "SPAIResult.h"
#include "SPAIValue.h"
#include "SPAIUnit.h"

#include "SPAIOptions.h"
#include "SPAIOptionName.h"
#include "SPAISystemInitGuard.h"

#include "SPAIArgumentParsingParasolid.h"

// SPAIParasolid
#include "SPAIParasolidDocument.h"

//#include "PsSession.h"
#include "parasolid_kernel.h"

//---------------------------------------------------------------------------------------

int main(int argc, char *argv[])
{
// SKY 6/1/06 - Licensing project

	SPAISystemInitGuard initGuard;

	unlock_license();

	int err = 0;
	if ( ( err = getArgs(argc, argv) ) !=0)
		return err;

	StartParasolid();

	SPAIResult result=SPAI_S_OK;

	SPAIDocument src(pszInputFile);
	if (strlen(pszInputFormat)>0)
		src.SetType(pszInputFormat);

	SPAIParasolidDocument dst;

	// Connect translates Parasolid entities in source unit by default. 
	// For Parasolid, it is highly recommended that the unit of Parasolid document should be meter.
	SPAIUnit outUnit( SPAIUnitMeter);
	dst.SetUnit( outUnit);

	SPAIFile console(1);

	SPAIOptions options;
	options.Add("SaveVersion",160);  // For ParaSolid

	SPAIConverter converter;
	result&=converter.SetOptions(options); 

	result&=converter.StartLog(console);
	result&=converter.Convert(src, dst);
	result&=converter.StopLog(console);

	int partCount=0;
	const PK_PART_t* pParts=NULL;
	result &= dst.GetParts(partCount, pParts);
	result &= dst.DetachEntities();

	PK_PART_transmit_o_t psSaveOpt;
	PK_PART_transmit_o_m(psSaveOpt);

	PK_SESSION_kernel_version_t version;
	PK_SESSION_ask_kernel_version(&version);

	psSaveOpt.transmit_version = version.major_revision*10;
	psSaveOpt.transmit_format = PK_transmit_format_text_c;
	psSaveOpt.transmit_user_fields = PK_LOGICAL_true;
	psSaveOpt.transmit_nmnl_geometry = PK_LOGICAL_false;

    PK_ERROR_code_t  status = PK_ERROR_wrong_format;

#ifdef _WINDOWS_SOURCE
	PK_UCHAR_t * unicodeFileName = ( PK_UCHAR_t*)pszOutputFile;
#else
	char asciiFileName[MAX_FILE_NAME_CHARS]="";
	int convertedChars=wcstombs( asciiFileName, pszOutputFile, MAX_FILE_NAME_CHARS );
//	const SPAXMILUniCodeChar * fileName = ( SPAXMILUniCodeChar*)fileCharName;
	if ( -1 == convertedChars)
	{
		result=SPAI_E_FAIL;
		return result;
	}
#endif

#ifdef _WINDOWS_SOURCE
    status = PK_PART_transmit_u(partCount, pParts, unicodeFileName, &psSaveOpt);
#else
    status = PK_PART_transmit(partCount, pParts, asciiFileName, &psSaveOpt);
#endif


	if (status!=PK_ERROR_no_errors) result &= SPAI_E_NO_FILE_SAVED;

	StopParasolid();

	printf("%s \n", result.GetMessage() );

	return result;
}

//---------------------------------------------------------------------------------------

void printHelp()
{
	printf("Usage : ParasolidImport -i file [-iformat format ] -o file\n");
	printf("Options :\n");
	printf("-i        : input file\n");
	printf("-o        : output (parasolid) file\n");
	printf("-iformat  : input file format\n");
}

//---------------------------------------------------------------------------------------
